<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="pd.showSearch">
        <el-form :model="pd.queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="pd.queryParams.name" placeholder="请输入名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="编号" prop="accountNo">
            <el-input v-model="pd.queryParams.accountNo" placeholder="请输入编号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="pd.queryParams.status" placeholder="请选择状态" clearable>
              <el-option v-for="dict in sys_status" :key="dict.value" :label="dict.name" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="更新时间" style="width: 308px">
            <el-date-picker
              v-model="dateRangeUpdateTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['fms:account:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="pd.single" @click="handleUpdate()" v-hasPermi="['fms:account:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="pd.multiple" @click="handleDelete()" v-hasPermi="['fms:account:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['fms:account:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="pd.loading" :data="pd.accountList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" v-if="true" />
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="编号" align="center" prop="accountNo" />
        <el-table-column label="当前余额" align="center" prop="balance" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="账户归属" align="center" prop="partnerId" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="更新时间" align="center" prop="updateTime" />
        <el-table-column fixed="left" label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['fms:account:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['fms:account:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="pd.total>0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                  v-model:limit="pd.queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改账户信息对话框 -->
    <el-dialog :title="pd.dialog.title" v-model="pd.dialog.visible" width="500px" append-to-body>
      <el-form ref="accountFormRef" :model="pd.form" :rules="pd.rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="pd.form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="编号" prop="accountNo">
          <el-input v-model="pd.form.accountNo" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="当前余额" prop="balance">
          <el-input v-model="pd.form.balance" placeholder="请输入当前余额" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="pd.form.status">
            <el-radio
              v-for="dict in sys_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.name}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="pd.form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="账户归属" prop="partnerId">
          <el-input v-model="pd.form.partnerId" placeholder="请输入账户归属" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="pd.form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="pd.buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Account" lang="ts">
import {AccountVO, listAccount, getAccount, delAccount, addAccount, updateAccount } from '@/views/fms/account/api';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_status } = toRefs<any>(proxy?.useDict('sys_status'));


const dateRangeUpdateTime = ref<[DateModelType, DateModelType]>(['', '']);

const queryFormRef = ref<ElFormInstance>();
const accountFormRef = ref<ElFormInstance>();

const pd = reactive({
  accountList: <AccountVO[]>([]),
  buttonLoading: false,
  loading: true,
  showSearch: true,
  selectVO: <AccountVO[]>([]),
  single: true,
  multiple: true,
  total: 0,
  form: <AccountVO>{},
  queryParams: <AccountVO>{
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    accountNo: undefined,
    status: undefined,
    params: {
      updateTime: undefined,
    }
  },
  rules: <ElFormRules>{
    name: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    accountNo: [
      { required: true, message: "编号不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  },
  dialog: <DialogOption>{
    visible: false,
    title: ''
  }
});

// const { queryParams, form, rules } = toRefs(data);

/** 查询账户信息列表 */
async function getList() {
  pd.loading = true;
  pd.queryParams.params = {};
  proxy?.addDateRange(pd.queryParams, dateRangeUpdateTime.value, 'UpdateTime');
  const res = await listAccount(pd.queryParams);
  pd.accountList = res.rows;
  pd.total = res.total;
  pd.loading = false;
}

/** 取消按钮 */
function cancel() {
  reset();
  pd.dialog.visible = false;
}

/** 表单重置 */
function reset() {
  pd.form = {};
  accountFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRangeUpdateTime.value = ['', ''];
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection: AccountVO[]) {
  pd.selectVO = selection;
  pd.single = selection.length != 1;
  pd.multiple = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  pd.dialog.visible = true;
  pd.dialog.title = "添加账户信息";
}

/** 修改按钮操作 */
async function handleUpdate(row?: AccountVO) {
  reset();
  const data = row || pd.selectVO[0]
  Object.assign(pd.form, data);
  pd.dialog.visible = true;
  pd.dialog.title = "修改账户信息";
}

/** 提交按钮 */
function submitForm() {
  accountFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      pd.buttonLoading = true;
      if (pd.form.id) {
        await updateAccount(pd.form).finally(() =>  pd.buttonLoading = false);
      } else {
        await addAccount(pd.form).finally(() =>  pd.buttonLoading = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      pd.dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
async function handleDelete(row?: AccountVO) {
  const _ids = row?.id || pd.selectVO.map(v => v.id);
  await proxy?.$modal.confirm('是否确认删除账户信息编号为"' + _ids + '"的数据项？').finally(() => pd.loading = false);
  await delAccount(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
function handleExport() {
  proxy?.download('fms/account/export', {
    ...pd.queryParams
  }, `account_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
